<?php
/*
 * 获取分页所需要的参数congcong
 * @param $page
 * @param $pagesize
 * @param $count
 * 
 * return array
 *   */
    function getPageArr($page,$pagesize=10,$count) {
    $page           = isset($page)?abs(intval($page)):1;
    $pagesize       = isset($pagesize)?abs(intval($pagesize)):10;
    $page > 100000 && $page = 100000;
    $pagesize > 100 && $page = 100;
    $pagetotal      = ceil($count / $pagesize);
    ($page>$pagetotal) && ($page = $pagetotal);
    ($page<1) && ($page = 1);
    
    //下一页
    if($page>$pagetotal){
      $nextpage=$pagetotal;
    }else{
        $nextpage=$page+1;
    }
    //上一页
    if($page>1){
      $prevpage=$page-1;
    }else{
      $prevpage=$page;
    }

    $pagearr = array(
        "page"      =>$page,
        "pagesize"  =>$pagesize,
        "pagetotal"=>$pagetotal,
        "count" =>$count,
        "start"=>($page - 1) * $pagesize,
        "nextpage"=>$nextpage,
        "prevpage"=>$prevpage

    );
    return $pagearr;
    }

    //检测用户是否已经登录
function is_login()
{
    $uid=!empty($_SESSION['uid'])?$_SESSION['uid']:'';
    if(!empty($uid))
    {
        return $uid;
    }else
    {
        return '';
    }
}

/**
*
 * 返回已经确定子分类的类别属性，计算等级
     *
     */
function getListCat($data = array(),$level = 0) {
    $cats = array();
    !$data && $data =getCats();
    if(!$data) return $cats;
    foreach($data as $key => $val) {
        $cats[$val['typeid']] = &$data[$key];
        $cats[$val['typeid']]['level'] = $level;
        if( isset( $val['children'] ) ){
            $cats[$val['typeid']]['haschild'] = 1;
            $cats += getListCat($val['children'],$level + 1);
            unset($data[$key]['children']);
        } 
    }
    return $cats;
}

/**
*
* 获取所有类别
*
*/
function getCats($return = 'tree') {
    static $retarr;
    if(isset($retarr[$return])) return $retarr[$return];

    $cats = M('type')->cache(true)->order('sort')->getField('typeid,typename,pid,sort');
    if($return == 'tree')
        {           //返回数组
            $tree = array();

            foreach($cats as $cat)
             {  //循环cat
                if(isset($cats[$cat['pid']])) 
                {//非第一级
                    $cats[$cat['pid']]['children'][] = &$cats[$cat['typeid']];
                } else
                 {//第一级
                    $tree[] = &$cats[$cat['typeid']];

                 }
             }
             $retarr[$return] = $tree;
         } else if($return == 'source')
         {//返回source
            $retarr[$return] = $cats;
         }
         return $retarr[$return];
     }

     //生成唯一订单号

function getOrderSn()
{
    /* 选择一个随机的方案 */
    mt_srand((double) microtime() * 1000000);
    /* 日期前缀+一个1-99999的五位随机数不满5未前位补0 */
    return date('Ymd').str_pad(mt_rand(1, 99999), 7, '0', STR_PAD_LEFT);
}